#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import logging
import json, time
from aiohttp import web
from aioweb import corelib

import db, main


routes = web.RouteTableDef()
module_name = 'records'

@routes.get('/%s' % module_name)
@corelib.template('records.html')
async def records_index(request):
    data = {}
    try:
        params = await corelib.parse_param(request)
        path = params.get('path', '/root')
    except BaseException as e:
        logging.error('\n%s' % corelib.printException(e))
    return data


@routes.get('/%s/items' % module_name)
async def records_items(request):
    data = {}
    try:
        params = await corelib.parse_param(request)
        index = int(params.get('index', '1'))
        pagesize = int(params.get('pagesize', '36'))
        session = db.create_session()
        page = await main.load_page('Records', index, pagesize=pagesize, order_by={'id': False})
        data = dict(data, **page)
    except BaseException as e:
        logging.error('\n%s' % corelib.printException(e))
        data['items'] = []
        data['page'] = {'page': {'total': 0, 'pages': 1, 'index': 1, 'size': pagesize}}
    return web.json_response({'code': 0, 'msg': data})


